package com.xiyu.service.repository.customer.pool;

import com.xiyu.service.model.customer.pool.*;
import com.xiyu.service.model.system.dept.SystemPostFetcher;
import org.babyfish.jimmer.spring.repository.JRepository;

import java.util.List;
import java.util.Optional;

public interface CustomerPoolMaxLimitRepository extends JRepository<CustomerPoolMaxLimit, Long> {
    CustomerPoolMaxLimitTable customerPoolMaxLimitTable = CustomerPoolMaxLimitTable.$;

//    default List<CustomerPoolMaxLimit> findByPostId(List<Long> postId){
//        return sql().createQuery(customerPoolMaxLimitTable)
//                .where(customerPoolMaxLimitTable.postIds().in(postId)
//                .select(customerPoolMaxLimitTable)
//                .execute();
//    }

    default Optional<CustomerPoolMaxLimit> findDetailById(Long id){
        return sql().createQuery(customerPoolMaxLimitTable)
                .where(customerPoolMaxLimitTable.id().eq(id))
                .select(customerPoolMaxLimitTable.fetch(CustomerPoolMaxLimitFetcher.$.allTableFields()
                        .limitPosts(CustomerPoolMaxLimitPostFetcher.$.allTableFields())))
                .fetchOptional();
    }

    default List<CustomerPoolMaxLimit> findDetail(){
        return sql().createQuery(customerPoolMaxLimitTable)
                .orderBy(customerPoolMaxLimitTable.id().desc())
                .select(customerPoolMaxLimitTable.fetch(CustomerPoolMaxLimitFetcher.$.allTableFields()
                        .limitPosts(CustomerPoolMaxLimitPostFetcher.$.allTableFields().post(SystemPostFetcher.$.name()))))
                .execute();
    }

    default List<CustomerPoolMaxLimit> findByPostId(Long postId){
        return sql().createQuery(customerPoolMaxLimitTable)
                .where(customerPoolMaxLimitTable.isEnable().eq(true))
                .where(customerPoolMaxLimitTable.asTableEx().limitPosts().postId().eq(postId))
                .select(customerPoolMaxLimitTable)
                .execute();
    }


}